package com.madzone.wiki.gae.ds.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UserGroup {
	/* jspwiki.groupdatabase.table=groups
	jspwiki.groupdatabase.membertable=group_members
	jspwiki.groupdatabase.created=created
	jspwiki.groupdatabase.creator=creator
	jspwiki.groupdatabase.name=name
	jspwiki.groupdatabase.member=member
	jspwiki.groupdatabase.modified=modified
	jspwiki.groupdatabase.modifier=modifier */
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	@Unique
	private String name;
	@Persistent
	private Date created;
	@Persistent
	private Key creator;
	@Persistent
	private Date modified;
	@Persistent
	private Key modifier;
	
	@Persistent
	private List<Key> members;
	
	public List<User> getMembers() {
		List<User> memberUsers = null;
		if (members != null) {
			PersistenceManager pm = PMF.getPM();
			Query query = pm.newQuery(User.class);
			query.declareImports("import com.google.appengine.api.datastore.Key");
			query.setFilter(":members.contains(key)");
			query.declareParameters("List<Key> members");
			memberUsers = (List<User>) query.execute(members);
		}
		return memberUsers;
	}
	public void setMembers(List<User> memberUsers) {
		if (memberUsers != null) {
			List<Key> memberKeys = new ArrayList<Key>();
			for (User user : memberUsers) {
				memberKeys.add(user.getKey());
			}
			this.members = memberKeys;
		} else {
			this.members = null;
		}
	}
	public Key getKey() {
		return key;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
	public User getCreator() {
		if (creator != null) {
			PersistenceManager pm = PMF.getPM();
			Query query = pm.newQuery(User.class);
			User user = pm.getObjectById(User.class, creator);
			return user;
		}
		return null;
	}
	public void setCreator(User creatorUser) {
		this.creator = creatorUser.getKey();
	}
	public void setCreator(String creatorUserName) {
		setCreator(User.getByFullName(creatorUserName));
	}
	public Date getModified() {
		return modified;
	}
	public void setModified(Date modified) {
		this.modified = modified;
	}
	public User getModifier() {
		if (modifier != null) {
			PersistenceManager pm = PMF.getPM();
			Query query = pm.newQuery(User.class);
			User user = pm.getObjectById(User.class, modifier);
			return user;
		}
		return null;
	}
	public void setModifier(User modifierUser) {
		this.modifier = modifierUser.getKey();
	}
	
	public void setModifier(String username) {
		setModifier(User.getByFullName(username));
	}
	
	public static UserGroup getByGroupName(String groupName) {
		String query = "select from " + UserGroup.class.getName() + " where name == '" + groupName + "'";
		PersistenceManager pm = PMF.getPM();
		UserGroup userGroup = null;
		try {
			List<UserGroup> groups = (List<UserGroup>) pm.newQuery(query).execute();
			if (groups.size() >= 1) {
				// INFO This is an error condition
				userGroup = groups.get(0);
			}
		} finally {
			//pm.close();
		}
		return userGroup;
	}
	
	public static List<UserGroup> getAllGroups() {
		String query = "select from " + UserGroup.class.getName();
		PersistenceManager pm = PMF.getPM();
		List<UserGroup> groups = null;
		try {
			groups = (List<UserGroup>) pm.newQuery(query).execute();
		} finally {
			//pm.close();
		}
		return groups;
	}

	public void save() {
		PersistenceManager pm = PMF.getPM();
		try {
			pm.makePersistent(this);
		} finally {
			//pm.close();
		}
	}
	
	public void delete() {
		PersistenceManager pm = PMF.getPM();
		try {
			pm.deletePersistent(this);
		} finally {
			//pm.close();
		}
	}
}
